package com.lekang.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.domain.Balance;
import com.lekang.service.IBalanceService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 余额Controller
 * 
 * @author ruoyi
 * @date 2025-10-15
 */
@RestController
@RequestMapping("/balance/balancefind")
public class BalanceController extends BaseController
{
    @Autowired
    private IBalanceService balanceService;

    /**
     * 查询余额列表
     */
    @PreAuthorize("@ss.hasPermi('balance:balancefind:list')")
    @GetMapping("/list")
    public TableDataInfo list(Balance balance)
    {
        startPage();
        List<Balance> list = balanceService.selectBalanceList(balance);
        return getDataTable(list);
    }

    /**
     * 导出余额列表
     */
    @PreAuthorize("@ss.hasPermi('balance:balancefind:export')")
    @Log(title = "余额", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Balance balance)
    {
        List<Balance> list = balanceService.selectBalanceList(balance);
        ExcelUtil<Balance> util = new ExcelUtil<Balance>(Balance.class);
        util.exportExcel(response, list, "余额数据");
    }

    /**
     * 获取余额详细信息
     */
    @PreAuthorize("@ss.hasPermi('balance:balancefind:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(balanceService.selectBalanceById(id));
    }

    /**
     * 新增余额
     */
    @PreAuthorize("@ss.hasPermi('balance:balancefind:add')")
    @Log(title = "余额", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Balance balance)
    {
        return toAjax(balanceService.insertBalance(balance));
    }

    /**
     * 修改余额
     */
    @PreAuthorize("@ss.hasPermi('balance:balancefind:edit')")
    @Log(title = "余额", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Balance balance)
    {
        return toAjax(balanceService.updateBalance(balance));
    }

    /**
     * 删除余额
     */
    @PreAuthorize("@ss.hasPermi('balance:balancefind:remove')")
    @Log(title = "余额", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(balanceService.deleteBalanceByIds(ids));
    }

    // 后端Controller示例代码
    @GetMapping("/byElderlyId/{elderlyId}")
    public AjaxResult getBalanceByElderlyIdS(@PathVariable Long elderlyId) {
        return AjaxResult.success(balanceService.getBalanceByElderlyId(elderlyId));
    }


    /**
     * 根据老人ID查询余额信息
     */
    @PreAuthorize("@ss.hasPermi('balance:balancefind:query')")
    @GetMapping(value = "/elderly/{elderlyId}")
    public AjaxResult getBalanceByElderlyId(@PathVariable("elderlyId") Long elderlyId) {
        return success(balanceService.selectBalanceByElderlyId(elderlyId));
    }

    /**
     * 根据老人id修改余额
     */
    @PreAuthorize("@ss.hasPermi('balance:balancefind:edit')")
    @Log(title = "余额", businessType = BusinessType.UPDATE)
    @PutMapping("/editByElderlyId")
    public AjaxResult editByElderlyId(@RequestBody Balance balance)
    {
        return toAjax(balanceService.updateBalanceByElderlyId(balance));
    }

}
